package com.yanqu.road.server.cmd.activity.tongbing.cross;

import com.yanqu.road.entity.player.UserHistoryAttribute;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.bussiness.player.UserHistoryAttributeBussiness;
import com.yanqu.road.pb.activity.TongBingProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_CROSS_TONG_BING_GET_UNION_MEMBER_REQ, desc = "")
public class TongBingGetUnionMemberReqCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        TongBingProto.CrossTongBingGetMemberReqMsg reqMsg = TongBingProto.CrossTongBingGetMemberReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        List<Long> userIdList = reqMsg.getUserIdList();
        int groupId = reqMsg.getGroupId();

        Map<Long, UserHistoryAttribute> attributeMap = null;
        attributeMap = UserHistoryAttributeBussiness.getUserHistoryAttributeMap(userIdList);

        TongBingProto.CrossTongBingGetMemberRespMsg.Builder builder = TongBingProto.CrossTongBingGetMemberRespMsg.newBuilder();
        builder.setActivityId(activityId);
        for (Long userId : userIdList) {
            UserInfo userInfo = UserMgr.getUserInfo(userId);
            if (userInfo != null) {

                // 仅用作查询，以跨服上的unionUid为准
                String unionUid = userInfo.getUnionUid();
                // 加入商会后还没上线，userInfo的似乎没刷新过来
                if (StringUtils.isNullOrEmpty(unionUid)) {
                    CrossUnionMember unionMember = CrossUnionMgr.getUnionMember(userInfo.getUserId());
                    if (unionMember != null) {
                        unionUid = unionMember.getUnionUid();
                    }
                }

                TongBingProto.TongBingUnionMemberMsg.Builder tmp = TongBingProto.TongBingUnionMemberMsg.newBuilder();
                tmp.setUserId(userId);
                tmp.setUnionUid(unionUid);
                tmp.setEarnSpeed(userInfo.getEarnSpeed().toString());
                if (attributeMap.containsKey(userId)) {
                    tmp.setPatronsTopAbility(attributeMap.get(userId).getAbility().toString());
                }
                builder.addMember(tmp);
            }
        }

        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_TONG_BING_GET_UNION_MEMBER_RESP, builder, 0, activityId, groupId);
    }
}
